<?php

namespace core\libs;

/*
 * 处理多图片上传， 返回上传成功后的图片名称数组。
 */
class multiple {
	private $upload_folder;//上传的目录	
	private $file_path;//文件保存路径
	private $max_file_size;//文件最大值
	private $upload_types = array();//文件类型限制
	private $upload_codes = array();//文件类型二进制字符串
	private $upload_files = array();//待上传文件数组
	public $upload_path = array();//上传成功文件数组
	public $upload_error = array();//上传错误文件数组
	
	//构造方法
	public function __construct($type = "pic") {
		// 获取上传配置
		$multiple_conf = config::get('plugins', 'UPLOAD');
		$this->upload_folder = $multiple_conf[$type]["upload_folder"];
		$this->file_path = $multiple_conf[$type]["file_path"];
		$this->max_file_size = $multiple_conf[$type]["max_file_size"];
		$this->upload_types = $multiple_conf[$type]["upload_types"];
		$this->upload_codes = $multiple_conf[$type]["upload_codes"];
	}
	
	//初始化
	public function init($upload_files) {
		$this->upload_path = array();
		$this->upload_error = array();
		$this->upload_files = $upload_files;
		if(!is_uploaded_file($this->upload_files["tmp_name"][0])) {
			alert("File is empty!");
			exit;
		}

		
		$up_files = $this->check_file();
		//提示 
		alert(count($up_files['path'])."个文件上传成功，".count($up_files['error'])."个文件上传失败！[".json_encode($up_files['error'])."]");
		if(count($up_files['path'])) {
			//上传成功，返回图片路径
			echo "<script type='text/javascript'>onBack('".$_GET['name']."', '" . implode(",", $up_files['path']) . "');</script>";
		}
	}
	
	//检查文件 
	private function check_file() {
		foreach($this->upload_files["tmp_name"] as $k => $val) {
			$file = $this->upload_file($k);
			if($file) {
				$this->upload_path[] = $file;
			}
		}
		
		return array(
			"path"  =>$this->upload_path,
			"error" =>$this->upload_error
		);
	}
	
	//上传文件
	private function upload_file($key) {
		//获取上传信息
		$file = $this->upload_files;		
		//图片信息
		$path_info = pathinfo($file["name"][$key]);	   
		//图片类型
		$file_type = $path_info['extension'];
		//图片完整路径
		$file_name = rand(10000, 99999) . $key . '_'. time()."." . $file_type;
		$file_path = $this->upload_folder . $file_name;	
		//检查文件大小
		if($this->max_file_size < $file["size"][$key]) {
			$this->upload_error[] = array("msg" => "文件太大");
			return false;
		}
		//检查文件类型
		if(!in_array($file["type"][$key], $this->upload_types)) {
			$this->upload_error[] = array("msg" => "文件类型不符 [".$file["type"][$key]."]!");
			return false;
		}


		//二进制代码验证文件类型
        $fp = fopen($file["tmp_name"][$key], "rb");
        $bin = fread($fp, 2); //只读2字节
        fclose($fp);
        $str_info = @unpack("C2chars", $bin); // unpack() 函数从二进制字符串对数据进行解包
        $type_code = intval($str_info['chars1'] . $str_info['chars2']); // intval() 函数用于获取变量的整数值
        if (!in_array($type_code, $this->upload_codes)) {
            $this->upload_error[] = array("msg" => "文件类型不符 [".$type_code."]!");
			return false;
        }

		//如果目录不存在则创建
		if(!file_exists($this->upload_folder)) {
			mkdir($this->upload_folder);
		}  
		//检查是否有相同文件
		if(file_exists($file_path))	{
			$this->upload_error[] = array("msg" => "同名文件已经存在了!");
			return false;
		}
		// 判断是否有权限
		if(!move_uploaded_file($file["tmp_name"][$key], $file_path)) {
			$this->upload_error[] = array("msg" => "移动文件出错!");
			return false;
		}
		
		return $this->file_path . $file_name;
	}
}

?>